<?php
// foreach ($lev2 as $key => $value) {
    // var_dump($value);
// }
// die;
?>
<style type="text/css" media="screen">
#main {
    width: 150px;
    /*margin:auto;*/
    /*background: #ececec;*/
    /*padding: 10px;*/
    /*border: 1px solid #ccc;*/
}

#image-list {
    list-style:none;
    margin:0;
    padding:0;
}
#image-list li {
    background: #fff;
    /*border: 1px solid #ccc;*/
    text-align:center;
    padding:10px;
    margin-bottom:10px;
}
#image-list li img {
    width: 150px;
    vertical-align: middle;
    border:1px solid #474747;
}
</style>
<div class="box round first">
        <h2><?php echo __('Admin Add Product'); ?></h2>
<div class="products form block">
    <div id="main">
 <div id="response">
<?php
echo $this->Form->create('Image', array('type' => 'file', 'url' => array('controller' => 'products', 'action' => 'add')));
echo $this->Form->input(__('Image'), array('type' => 'file', 'id'=>'images'));
echo $this->Form->input(__('Choice'), array('id'=>'btn', 'type'=>'button', 'label' => FALSE));
echo $this->Form->end();
?>
    </div>
        <ul id="image-list">
        </ul>
    </div>

<?php echo $this->Form->create('Product'); ?>
    <fieldset>
    <?php
        echo $this->Form->input('code');
        echo $this->Form->input('name');
        echo $this->Form->input('provider_id');
        // echo $this->Form->input('category_id');
       // echo $this->Form->input('category_id', array('id' => 'CategorySub1', 'options' => $lev1, 'empty' => FALSE, 'div'=>array('id'=>'lev1')));
?>

<div class="input select required"><label for="ProductCategoryId">Category</label>
    <select id="ProductCategoryId" name="data[Product][category_id]">
<?php foreach($lev1 as $key => $lev11): ?>
    <?php if(!in_array($key, $sub2)){?>
            <option value="<?php echo $key; ?>"><?php echo $lev11; ?></option>
    <?php }else{?>
       <optgroup label="<?php echo $lev11; ?>">
       <?php     foreach ($lev2 as $lev21) {if($lev21['Category']['sub']==$key){ if(!in_array($lev21['Category']['id'], $sub3)){?>

        <option value="<?php echo $lev21['Category']['id']; ?>"><?php echo $lev21['Category']['title']; ?></option>
      <?php }}}?>
       </optgroup>
       <?php foreach ($lev2 as $lev21) {if($lev21['Category']['sub']==$key){ if(in_array($lev21['Category']['id'], $sub3)){?>
        <optgroup class="suboptgroup" label="<?php echo $lev21['Category']['title']; ?>">
            <?php foreach ($lev3 as $lev31) { if($lev31['Category']['sub']==$lev21['Category']['id']){ ?>
                    <option value="<?php echo $lev31['Category']['id']; ?>"><?php echo $lev31['Category']['title']; ?></option>
            <?php }} ?>
        </optgroup>

      <?php }}}?>

   <?php } ?>

<?php endforeach ?>

    </select>
</div>


    <?php
        echo $this->Form->input('price');
        echo $this->Form->input('describe');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
</div>
    <script type="text/javascript">
        (function () {
    var input = document.getElementById("images"),
        formdata = false;

    function showUploadedItem (source) {
        var list = document.getElementById("image-list"),
            li   = document.createElement("li"),
            img  = document.createElement("img");
        img.src = source;
        li.appendChild(img);
        list.appendChild(li);
    }

    if (window.FormData) {
        formdata = new FormData();
        document.getElementById("btn").style.display = "none";
    }

    input.addEventListener("change", function (evt) {
        document.getElementById("response").innerHTML = "Uploading . . ."
        var i = 0, len = this.files.length, img, reader, file;

        for ( ; i < len; i++ ) {
            file = this.files[i];

            if (!!file.type.match(/image.*/)) {
                if ( window.FileReader ) {
                    reader = new FileReader();
                    reader.onloadend = function (e) {
                        showUploadedItem(e.target.result, file.fileName);
                    };
                    reader.readAsDataURL(file);
                }
                if (formdata) {
                    formdata.append("images[]", file);
                }
            }
        }

        if (formdata) {
            $.ajax({
                async:true,
                url: "<?php echo Router::url(array('controller'=>'products','action'=>'add')) ?>",
                type: "POST",
                data: formdata,
                processData: false,
                contentType: false,
                success: function (res) {
                    document.getElementById("response").innerHTML = res;
                }
            });
        }
    }, false);
}());
    </script>
